home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elib-006.lha / elib-0.06 / library / read.el < prev    next >
Lisp/Scheme  |  1993-01-24  |  3KB  |  113 lines

  1. ;;;; $Id: read.el,v 0.5 1992/08/19 01:57:53 ceder Exp $
  2. ;;;; This file contains a number of functions for reading from the
  3. ;;;; minibuffer.
  4. ;;;;
  5. ;;;; Copyright (C) 1991, 1992 Free Software Foundation
  6. ;;;;
  7. ;;;; This file is part of the GNU Emacs lisp library, Elib.
  8. ;;;;
  9. ;;;; GNU Elib is free software; you can redistribute it and/or modify
  10. ;;;; it under the terms of the GNU General Public License as published by
  11. ;;;; the Free Software Foundation; either version 1, or (at your option)
  12. ;;;; any later version.
  13. ;;;;
  14. ;;;; GNU Elib is distributed in the hope that it will be useful,
  15. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;;;; GNU General Public License for more details.
  18. ;;;;
  19. ;;;; You should have received a copy of the GNU General Public License
  20. ;;;; along with GNU Emacs; see the file COPYING.  If not, write to
  21. ;;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22. ;;;;
  23. ;;;; Authors: Inge Wallin, Lars Willf|r
  24. ;;;;
  25.  
  26.  
  27. (provide 'read)
  28.  
  29.  
  30. (defun read-number (&optional prompt default)
  31.   "Read a number from the minibuffer. 
  32. Optional arguments: PROMPT DEFAULT.
  33.  
  34. If DEFAULT is non-nil, it is written within parenthesis after the prompt.
  35. DEFAULT can be either a number, or of the type which `(interactive P)'
  36. generates." 
  37.   (let ((numdefault (cond ((null default) nil)
  38.               ((integerp default) default)
  39.               ((listp default) (car default))
  40.               (t nil)))
  41.     (number nil)
  42.     (numstr nil))
  43.  
  44.     (while (not number)
  45.       (setq numstr (read-string
  46.             (concat (if prompt 
  47.                 prompt
  48.                   "Enter a number: ")
  49.                 (if numdefault 
  50.                 (format "(%d) " numdefault)))))
  51.       (cond ((and (string= numstr "") 
  52.           numdefault)
  53.          (setq number numdefault))
  54.         ((string-match "\\`[0-9]+\\'" numstr)
  55.          (setq number (string-to-int numstr)))
  56.         (t (beep))))
  57.     number))
  58.  
  59.  
  60. (defun read-num-range (low high &optional prompt show-range)
  61.   "Read a number within an interval from the minibuffer.
  62. Args: LOW HIGH &optional PROMPT SHOW-RANGE.
  63.  
  64. The number read must be within the range [LOW HIGH].
  65. If SHOW-RANGE is non-nil, the prompt will include the range for information
  66. to the user."
  67.   (let ((number (1- low)))
  68.     (while (or (< number low)
  69.            (> number high))
  70.       (setq number (read-number 
  71.             (concat (if prompt
  72.                 prompt
  73.                   "Enter a number: ")
  74.                 (if show-range
  75.                 (format "(%d-%d) " low high)
  76.                   "")))))
  77.     number))
  78.  
  79.  
  80. (defun read-silent (prompt &optional outchar)
  81.   "Read a string in the minibuffer without echoing.
  82. Args: PROMPT &optional OUTCHAR.
  83.  
  84. For each character the user writes, one OUTCHAR is displayed."
  85.   (message prompt)
  86.   (let ((input-string "")
  87.     (input-char)
  88.     (cursor-in-echo-area t)
  89.     (showstring ""))
  90.     (while (not (or (eq (setq input-char (read-char)) ?\r)
  91.             (eq input-char ?\n)))
  92.       (cond
  93.        ((eq input-char ?\C-?)
  94.     (if (equal (length input-string)
  95.            0)
  96.         nil
  97.       (setq showstring (substring showstring 0 -1))
  98.       (setq input-string (substring input-string 0 -1))))
  99.        ((eq input-char ?\C-u)
  100.     (setq showstring "")
  101.     (setq input-string ""))
  102.        (t
  103.     (if outchar
  104.         (setq showstring 
  105.           (concat showstring (char-to-string outchar))))
  106.     (setq input-string
  107.           (concat input-string (char-to-string input-char)))))
  108.       (message (concat prompt (if outchar
  109.                   showstring
  110.                 nil))))
  111.     (message "")
  112.     input-string))
  113.